package leetcode;

import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.List;

public class p144二叉树的前序遍历 {
    //将前序遍历结果放到list中
    public List<Integer> preorderTraversal(TreeNode root) {
        preorderList.clear();
        preorder(root);
        return preorderList;

    }

    private List<Integer> preorderList=new ArrayList<>();
    //每次调用记得初始化preoderList！！！！


    private void preorder(TreeNode root) {
        if (root==null) {
            return;
        }

        //System.out.printf("%c ",root.val);
        preorderList.add(root.val);
        preorder(root.left);
        preorder(root.right);
    }
}
